import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
} from '@nestjs/common';
import { Response, Request } from 'express';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const reponse = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();
    const status = exception.getStatus();

    reponse.status(status).json({
      statusCode: status,
      data: exception.message,
      time: new Date(),
      success: status >= 200 && status < 300 ? 'OK' : 'Error',
      path: request.url,
    });
  }
}
